//
//  MoodCell.m
//  VICKY ZONE
//
//  Created by sunx on 15/3/24.
//  Copyright (c) 2015年 sunx. All rights reserved.
//

#import "MoodCell.h"
#import "SVProgressHUD.h"
#import "NSURLSession+KKAddition.h"

#define MANAGER @"http://samoyun.com/controller/mainController.php"
@interface MoodCell()
@property(nonatomic,strong) NSString *username;
@property(nonatomic) BOOL isEditing;
@end
@implementation MoodCell
-(instancetype)initWithString:(NSString *)string {
    self = [super init];
    UIColor *textColor = [UIColor colorWithRed:189.0/255.0 green:189.0/255.0 blue:189.0/255.0 alpha:1];
    
    NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
    self.username = [userDefault valueForKey:@"username"];
    self.isEditing = NO;
    
    self.backgroundColor = [UIColor colorWithRed:0.2 green:0.2 blue:0.2 alpha:0.4];
    CGRect rect = [ UIScreen mainScreen ].applicationFrame;
    float height = rect.size.height;
    float width = rect.size.width;
    
    self.textView = [[UITextView alloc] init];
    self.textView.text = string;
    
    self.textView.font = [UIFont fontWithName:@"Helvetica" size:15];
    self.textView.textColor = textColor;
    /*
    //计算text view高度
    float bodyHeight;
    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7) {
        
        CGRect textFrame=[[self.textView layoutManager]usedRectForTextContainer:[self.textView textContainer]];
        bodyHeight = textFrame.size.height;
        
    }else {
        bodyHeight = self.textView.contentSize.height;
    }
    bodyHeight += 60;

     */

    self.button = [[UIButton alloc] init];
    self.button.frame = CGRectMake(width * 0.8, 10,50,20);
    self.button.backgroundColor = [UIColor colorWithRed:0.2 green:0.2 blue:0.2 alpha:1];
    self.button.layer.cornerRadius = 4;
    self.button.titleLabel.font = [UIFont systemFontOfSize:14];
    self.button.titleLabel.textColor = [UIColor grayColor];
    
    self.button.layer.shadowColor = [UIColor blackColor].CGColor;
    self.button.layer.shadowOffset = CGSizeMake(1.0, 1.0);
    self.button.layer.shadowOpacity = 1.0f;
    
    
    [self.button setTitle:@"edit" forState:UIControlStateNormal];
    [self.button addTarget:self action:@selector(edit) forControlEvents:UIControlEventTouchDown];
    [self addSubview:self.button];
    
    self.textView.backgroundColor = [UIColor clearColor];
    self.textView.frame = CGRectMake(width * 0.1, height *0.05, width * 0.8, height*0.13);
    
    [self addSubview:self.textView];
    self.height = height*0.2;
    return self;
}
- (void)edit {
    if(!self.isEditing) {
        [self.textView becomeFirstResponder];
        self.isEditing = YES;
    }else {
        [SVProgressHUD show];
        NSString *mood = self.textView.text;
        
        [NSURLSession POST:MANAGER pararms:@{@"user":self.username,@"mood":mood} completed:^(NSData *data) {
            [self performSelectorOnMainThread:@selector(HUDDissmiss) withObject:self waitUntilDone:YES];
            if(data == nil) {
                NSLog(@"success");
            }else {
                NSLog(@"%@",[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]);
            }
        } failed:^(NSError *error) {
            if(error) {
                NSLog(@"%@",error);
                [self performSelectorOnMainThread:@selector(HUDDissmiss) withObject:self waitUntilDone:YES];
            }
        }];
        [[[UIApplication sharedApplication] keyWindow] endEditing:YES];

    }
}
- (void)HUDDissmiss {
    [SVProgressHUD dismiss];
}
@end
